package com.qd.core.security.provider;

import com.qd.core.security.token.BaseAuthenticationToken;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;

@Slf4j
public class NoPwdDaoAuthenticationProvider implements AuthenticationProvider {
    private UserDetailsService userDetailsService;


    @Override
    public Authentication authenticate(Authentication authentication) throws AuthenticationException {
        log.info("NoPwdDaoAuthenticationProvider,name: {},principal: {},credentials: {}", authentication.getName(), authentication.getPrincipal(), authentication.getCredentials());
        final UserDetails user = userDetailsService.loadUserByUsername(authentication.getName());
        return new BaseAuthenticationToken(user, authentication.getCredentials(), user.getAuthorities());
    }

    @Override
    public boolean supports(Class<?> authentication) {
        return BaseAuthenticationToken.class.isAssignableFrom(authentication);
    }

    public UserDetailsService getUserDetailsService() {
        return userDetailsService;
    }

    public void setUserDetailsService(UserDetailsService userDetailsService) {
        this.userDetailsService = userDetailsService;
    }
}

